package main;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFReportTest {

	public static void main(String[] args) throws Exception, DocumentException {

		List<String> ponum = new ArrayList<String>();
		add(ponum, 2600);
		List<String> line = new ArrayList<String>();
		add(line, 2600);
		List<String> part = new ArrayList<String>();
		add(part, 2600);
		List<String> description = new ArrayList<String>();
		add(description, 2600);
		List<String> origin = new ArrayList<String>();
		add(origin, 2600);
		Rectangle rectPageSize = new Rectangle(PageSize.A4);// A4纸张
		Document document = new Document(rectPageSize, 40, 40, 100, 40);// 上、下、左、右间距
		/*
		 * //Create Document Instance Document document=new Document();
		 */

		// add Chinese font
		// BaseFont bfChinese=BaseFont.createFont("STSong-Light",
		// "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		final BaseFont bfChinese = BaseFont.createFont(
				"C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H,
				BaseFont.EMBEDDED);

		// Font headfont=new Font(bfChinese,10,Font.BOLD);
		Font keyfont = new Font(bfChinese, 8, Font.BOLD);
		Font textfont = new Font(bfChinese, 8, Font.NORMAL);

		// Create Writer associated with document
		FileOutputStream out = new FileOutputStream(new File(
				"E:\\POReceiveReport.pdf"));

		PdfWriter writer = PdfWriter.getInstance(document, out);

		PdfReportM1HeaderFooter header = new PdfReportM1HeaderFooter();
		header.setHeader("标题信息");
		header.setBf(bfChinese);
		writer.setPageEvent(header);

		/*
		 * writer.setPageEvent(new PdfPageEventHelper() { PdfTemplate total;
		 * public void onOpenDocument(PdfWriter writer, Document document) {
		 * total = writer.getDirectContent().createTemplate(30, 16); } public
		 * void onEndPage(PdfWriter writer, Document document) {
		 * 
		 * PdfContentByte cb = writer.getDirectContent(); cb.saveState();
		 * 
		 * cb.beginText(); BaseFont bf = bfChinese;
		 * 
		 * cb.setFontAndSize(bf, 10);
		 * 
		 * // Header float x = document.top(-20);
		 * 
		 * // 左 cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "H-Left",
		 * document.left(), x, 0); // 中
		 * cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
		 * writer.getPageNumber() + " 页 共", (document.right() + document.left())
		 * / 2, x, 0); // 右 cb.showTextAligned(PdfContentByte.ALIGN_RIGHT,
		 * "H-Right", document.right(), x, 0);
		 * 
		 * // Footer float y = document.bottom(-20);
		 * 
		 * // 左 cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "F-Left",
		 * document.left(), y, 0); // 中
		 * cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
		 * writer.getPageNumber() + " 页", (document.right() + document.left()) /
		 * 2, y, 0); // 右 cb.showTextAligned(PdfContentByte.ALIGN_RIGHT,
		 * "F-Right", document.right(), y, 0);
		 * 
		 * cb.endText();
		 * 
		 * cb.restoreState(); }
		 * 
		 * public void onCloseDocument(PdfWriter writer, Document document) { //
		 * ColumnText.showTextAligned(headertemp,Element.ALIGN_LEFT,new //
		 * Phrase(String.valueOf(writer.getPageNumber())+" 页"),2,2,0); //
		 * ColumnText.showTextAligned(footertemp,Element.ALIGN_LEFT,new //
		 * Phrase(String.valueOf(writer.getPageNumber())+" 页"),2,2,0);
		 * PdfContentByte cb = writer.getDirectContent(); cb.saveState();
		 * 
		 * cb.beginText(); BaseFont bf = bfChinese; cb.setFontAndSize(bf, 10);
		 * float x = document.top(-20); float y = document.bottom(-20);
		 * ColumnText.showTextAligned(total,Element.ALIGN_CENTER,new
		 * Phrase(String
		 * .valueOf(writer.getPageNumber())+" weeeeedfseeeeeeeeeeeeeeeeeeeeeeeeee"
		 * ),200,100,0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
		 * (String.
		 * valueOf(writer.getPageNumber())+"s111111111111111111111114444444444 页"
		 * ), (document.right() + document.left()) / 2+20, x, 0);
		 * cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
		 * (String.valueOf(writer
		 * .getPageNumber())+"s111111111111111111111114444444444 页"),
		 * (document.right() + document.left()) / 2+20, y, 0); cb.endText();
		 * 
		 * cb.restoreState(); } });
		 */

		document.open();

		// record header field
		PdfPTable t = new PdfPTable(5);
		float[] widths = { 1.5f, 1f, 1f, 1.5f, 1f };
		t.setWidths(widths);
		t.setTotalWidth(100);
		t.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
		PdfPCell c1 = new PdfPCell(new Paragraph("PO#", keyfont));
		t.addCell(c1);
		c1 = new PdfPCell(new Paragraph("Line", keyfont));
		t.addCell(c1);
		c1 = new PdfPCell(new Paragraph("Part#", keyfont));
		t.addCell(c1);
		c1 = new PdfPCell(new Paragraph("Description", keyfont));
		t.addCell(c1);
		c1 = new PdfPCell(new Paragraph("Origin", keyfont));
		t.addCell(c1);
		t.setHeaderRows(1);
		int _MAX_ROWS = 1000;// 最大行数，之后清理
		int row_count = 0;// 初始值
		for (int i = 0; i < line.size(); i++) {
			PdfPCell c2 = new PdfPCell(new Paragraph(ponum.get(i), textfont));
			t.addCell(c2);
			c2 = new PdfPCell(new Paragraph(line.get(i), textfont));
			t.addCell(c2);
			c2 = new PdfPCell(new Paragraph(part.get(i), textfont));
			t.addCell(c2);
			c2 = new PdfPCell(new Paragraph(description.get(i), textfont));
			t.addCell(c2);
			c2 = new PdfPCell(new Paragraph(origin.get(i), textfont));
			t.addCell(c2);

			if (++row_count % _MAX_ROWS == 0) {
				// datatable是我的一个PdfPTable的new出来的一个实例
				// add table to Document
				document.add(t);
				// delete _MAX_ROWS from table to free memory
				t.deleteBodyRows();
				// let iText manage when table header written
				t.setSkipFirstHeader(true);// 防止释放后一页出现两次表头。
				document.newPage();
				header.setHeader("标题信息 新一页"+row_count/_MAX_ROWS);
			}
		}
		if(++row_count % _MAX_ROWS > 0){
			document.add(t);
		}
		
		document.close();
	}

	public static String leftPad(String str, int i) {
		int addSpaceNo = i - str.length();
		String space = "";
		for (int k = 0; k < addSpaceNo; k++) {
			space = " " + space;
		}
		;
		String result = space + str;
		return result;
	}

	public static void add(List<String> list, int num) {
		for (int i = 0; i < num; i++) {
			list.add("test from Foster "
					+ i);
		}
	}

	public static String printBlank(int tmp) {
		String space = "";
		for (int m = 0; m < tmp; m++) {
			space = space + " ";
		}
		return space;
	}

}
